Skip to content

Move workerd bench's report building and startup timing to Rust - #8677

Merged
bartlomieju merged 4 commits into
mainfrom
biwanczuk/bench-rust
Nov 20, 2026
Merged

bartlomieju merged 4 commits into
mainfrom
biwanczuk/bench-rust

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

This is the first step of porting workerd bench from C++ to Rust. Only
workerd bench dispatches the bench event, so nothing a deployed Worker
can reach changes, which makes it a low-risk subcommand to work out the
approach on.

A new crate, bench-core, now builds a group's report (overhead
subtraction, statistics and flags) and runs the loop that times a
Worker's startup. The C++ side keeps a thin shim that hands results to
Rust and copies the encoded report back, and the case filter is still
kj::GlobFilter. bench-core has no JSG dependency, so both the worker
side and workerd-server can use it; BenchController follows in its
own crate.

No behavior change is intended, apart from a failed Worker start's error
description, which now shows a placeholder for invalid UTF-8 instead of
raw bytes that workerd bench couldn't print. The existing tests move
to Rust with the same inputs and expected values, and the bench tests
and @smoke targets pass unchanged.

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The port reads as faithful to the C++, and the GlobFilter port matches kj::GlobFilter line for line. Two things on the new fillBenchReport() shim, inline.

Comment thread src/workerd/api/bench.c++ Resolved
Comment thread src/workerd/api/BUILD.bazel Resolved
Move the code that turns a group's samples into a BenchReport.Group
out of api/bench.c++ into a new crate, bench-core
(src/workerd/api/bench-core/): subtracting the runner's overhead,
the HDR statistics, the atFloor and highVariance flags, and failing
cases with calls longer than an hour. This is the first step of porting
`workerd bench` to Rust. The crate has no JSG dependency, because
api/bench.c++ is built into the C++ io library, which the controller's
crate will depend on through io-context.

fillBenchReport() becomes a shim: it hands the results to Rust through
a cxx bridge and copies the encoded group into the caller's builder.
Case names and errors cross as bytes and are written to the report's
Text fields unchanged, since C++ strings needn't be valid UTF-8. A
case's error is set by a separate call, so an empty error stays set,
as with the kj::Maybe<kj::String> before.

The shim reads the encoded group with no traversal limit: a group holds
up to MAX_SAMPLES wall and CPU samples per case, which can be more than
capnp's default limit of 8M words, as when the report was written
straight to the builder before.

The fillBenchReport tests move from bench-test.c++ to
bench-core/lib-test.rs with the same inputs and expected values;
ffi-test.rs pins the bridge's behavior. bench-test.c++ keeps two tests
of the shim itself: a round trip of a group through it, and a group
larger than the default traversal limit. The C++ median() stays, with a
TODO, for the overhead measurement.

No behavior change.
@bartlomieju
bartlomieju marked this pull request as ready for review November 20, 2026 10:19
@bartlomieju
bartlomieju requested review from a team as code owners November 20, 2026 10:19

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of fb391e79.

This review, of b2665eda

💬 Approved with comments — The port matches the C++ it replaces, and the traversal-limit fix and the new shim tests are in place. Two INFO notes remain: peak memory on very large groups, and a median doc comment that doesn't match its code.

Findings: 2 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 2 info

The port matches the C++ and I found no bugs. Two INFO notes: memory for large groups and a median doc comment.

Recommendation: 💬 Optional Nits

  • [INFO] Encoding copies the full message a second time (bytes into a Vec<u64>), which adds to the peak memory of large groups. (src/workerd/api/bench-core/lib.rs:149)
  • [INFO] median's doc comment says the input must not be empty, but the function returns NaN for empty input. (src/workerd/api/bench-core/lib.rs:105)
View full review

The new bench-core crate matches the C++ it replaces. The traversal-limit issue raised earlier in the discussion is fixed: fillBenchReport() now reads the encoded group with traversalLimitInWords = kj::maxValue. The shim also has round-trip and large-group tests in bench-test.c++.

  • INFO: The report path now makes several full copies of each group's samples:

    • add_case copies them with to_vec.
    • subtract creates an intermediate vector.
    • The encoded message is written as bytes, then collected again into Vec<u64>.
    • setCases deep-copies the result into the C++ builder.

    This happens once per group and only in workerd bench, but with MAX_SAMPLES cases it can reach several hundred MB at peak.

  • INFO: The doc comment on median says the input must not be empty, but the function returns NaN for empty input. The C++ version asserted instead.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security concerns. Only workerd bench dispatches the bench event, and the new FFI surface takes byte slices and plain values.


📖 AGENTS.md: ✅ LGTM

No issues. The JS benchmarks row in the root AGENTS.md could mention src/workerd/api/bench-core/ in a later pull request.


⚡ Performance: ✅ LGTM

Report building runs once per group, at the end, and only in workerd bench. Its transient peak memory is covered in the code quality section.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: b2665eda

Comment thread src/workerd/api/bench-core/lib.rs Resolved
Comment thread src/workerd/api/bench-core/lib.rs Resolved
Move the code that turns a group's samples into a BenchReport.Group
out of api/bench.c++ into a new crate, bench-core
(src/workerd/api/bench-core/): subtracting the runner's overhead,
the HDR statistics, the atFloor and highVariance flags, and failing
cases with calls longer than an hour. This is the first step of porting
`workerd bench` to Rust. The crate has no JSG dependency, because
api/bench.c++ is built into the C++ io library, which the controller's
crate will depend on through io-context.

fillBenchReport() becomes a shim: it hands the results to Rust through
a cxx bridge and copies the encoded group into the caller's builder.
Case names and errors cross as bytes and are written to the report's
Text fields unchanged, since C++ strings needn't be valid UTF-8. A
case's error is set by a separate call, so an empty error stays set,
as with the kj::Maybe<kj::String> before.

The shim reads the encoded group with no traversal limit: a group holds
up to MAX_SAMPLES wall and CPU samples per case, which can be more than
capnp's default limit of 8M words, as when the report was written
straight to the builder before.

The fillBenchReport tests move from bench-test.c++ to
bench-core/lib-test.rs with the same inputs and expected values;
ffi-test.rs pins the bridge's behavior. bench-test.c++ keeps two tests
of the shim itself: a round trip of a group through it, and a group
larger than the default traversal limit. The C++ median() stays, with a
TODO, for the overhead measurement.

No behavior change.
Move the loop that times a Worker's startup for a startup group
(BenchReport.Group.startup) from api/bench.c++ into bench-core's new
startup module: the warmup, the sample count and sampling budget, the
--quick caps, the case filter on the phases, and failing the group when
a start fails. workerd-server calls it from WorkerService::bench_startup().

The factory now only starts one throwaway copy of the Worker and
returns how long its phases took: factory_bench_startup() becomes
factory_start_throwaway_worker(), which returns a WorkerStartupTiming
shared struct instead of an encoded group. The clocks are still read in
the factory, between the phases. A start that fails gives its
kj::Exception description as the group's error, as before.

The case filter is kj::GlobFilter itself, which bench-core reaches
through a small cxx bridge (bench-core's GlobFilter), so case names
match exactly as in C++. BenchController's own case filter will use it
once it moves to Rust.

The startup tests move from bench-test.c++ to
bench-core/startup-test.rs with the same inputs and expected values;
bench-test.c++ keeps the tests of the fillBenchReport() shim.
The defaults the loop shares with BenchController stay duplicated in
api/bench.c++, with a TODO, until BenchController moves.

No behavior change.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 2474e223.

This review, of fb391e79

💬 Approved with comments — No correctness, security or performance issues. The earlier notes are fixed, and one INFO note remains about the crate doc comment.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

The crate doc comment overstates where names and errors come from

Recommendation: 💬 Optional Nits

  • [INFO] Crate doc says report names and errors all come from C++ strings, but the startup module makes its phase names in Rust and takes errors as String. (src/workerd/api/bench-core/lib.rs:15)
View full review
  • lib.rs:15-16 says names and errors come from C++ strings. On the startup path, though, startup::time_startup creates the phase names in Rust and takes start errors as String.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security concerns. Only workerd bench dispatches the bench event.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

Both earlier notes are fixed. to_words (lib.rs:154-161) sizes its buffer with compute_serialized_size_in_words and writes the message straight into a Vec<u64>, so the second copy as bytes is gone. The median doc comment now matches its code, and the empty case has a test.

In startup.rs, the loop handles the warmup, STARTUP_MIN_SAMPLES and the sampling budget. A failed start is written as the group's error. An empty filter becomes *, and a filter that selects no phase returns before starting anything.


⚡ Performance: ✅ LGTM

Writing the encoded message straight into a pre-sized word buffer removes one full copy, which lowers peak memory for large groups. In startup.rs, timings is allocated up front.


Reviewed commit: fb391e79

Comment thread src/workerd/api/bench-core/lib.rs Resolved
The crate doc said all names and errors come from C++ strings, but the
startup module makes its own phase names and gets a failed start's error
as a String.

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found: the crate doc note from the last review is fixed, and the traversal-limit fix and its shim tests are in place.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. Only workerd bench dispatches the bench event, so no deployed Worker can reach this code. The report the shim decodes comes from the process's own encoder.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

to_words sizes its buffer with compute_serialized_size_in_words and writes the message straight into a Vec<u64>. The C++ shim reads that buffer as aligned words. The median doc comment matches its code, and stddev is scaled the same way as in the removed C++ code.


⚡ Performance: ✅ LGTM

The shim's copies are linear and run once per group, after sampling ends. Writing the encoded message straight into a pre-sized word buffer avoids an extra copy.


📝 Documentation: ✅ LGTM

The crate doc (lib.rs:15-18) now correctly separates bytes that come through the C++ bridge from the phase names and errors the startup module handles as Rust strings.


Reviewed commit: 2474e223

@bartlomieju
bartlomieju merged commit b8864a6 into main Nov 20, 2026
23 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/bench-rust branch November 20, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants